/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewlib.interpolator.Interpolator;
import com.hookedonplay.decoviewlib.interpolator.LinearInterpolator;
import com.hookedonplay.decoviewlib.interpolator.AnticipateInterpolator;
import com.hookedonplay.decoviewlib.interpolator.AccelerateInterpolator;
import com.hookedonplay.decoviewlib.interpolator.DecelerateInterpolator;
import com.hookedonplay.decoviewlib.interpolator.BounceInterpolator;
import com.hookedonplay.decoviewlib.interpolator.OvershootInterpolator;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.utils.Color;

public class SampleInterpolatorsMainAbility extends Ability {
    private int mSeries1Index;

    private DecoView dynamicArcView1, dynamicArcView2, dynamicArcView3, dynamicArcView4, dynamicArcView5, dynamicArcView6;
    
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_interpolators);

        dynamicArcView1 = (DecoView) findComponentById(ResourceTable.Id_dynamicArcView1);
        dynamicArcView2 = (DecoView) findComponentById(ResourceTable.Id_dynamicArcView2);
        dynamicArcView3 = (DecoView) findComponentById(ResourceTable.Id_dynamicArcView3);
        dynamicArcView4 = (DecoView) findComponentById(ResourceTable.Id_dynamicArcView4);
        dynamicArcView5 = (DecoView) findComponentById(ResourceTable.Id_dynamicArcView5);
        dynamicArcView6 = (DecoView) findComponentById(ResourceTable.Id_dynamicArcView6);
    }

    @Override
    public void onActive() {
        super.onActive();

        createTracks();
        setupEvents();
    }

    private void createTracks() {
        createTracks(dynamicArcView1, new LinearInterpolator(), RgbPalette.parse("#CC0000"));
        createTracks(dynamicArcView2, new AnticipateInterpolator(), RgbPalette.parse("#048482"));
        createTracks(dynamicArcView3, new AccelerateInterpolator(), RgbPalette.parse("#003366"));
        createTracks(dynamicArcView4, new DecelerateInterpolator(), RgbPalette.parse("#66A7C5"));
        createTracks(dynamicArcView5, new BounceInterpolator(), RgbPalette.parse("#FF6000"));
        createTracks(dynamicArcView6, new OvershootInterpolator(), RgbPalette.parse("#6F0564"));
    }

    private void setupEvents() {
        setupEvents(dynamicArcView1);
        setupEvents(dynamicArcView2);
        setupEvents(dynamicArcView3);
        setupEvents(dynamicArcView4);
        setupEvents(dynamicArcView5);
        setupEvents(dynamicArcView6);
    }

    private void createTracks(DecoView decoView, Interpolator interpolator, int color) {
        decoView.deleteAll();
        decoView.configureAngles(320, 180);

        final float mSeriesMax = 50f;
        SeriesItem arcBackTrack = new SeriesItem.Builder(Color.argb(255, 228, 228, 228))
                .setRange(0, mSeriesMax, mSeriesMax)
                .setLineWidth(Utility.getDimension(12f, getApplicationContext()))
                .build();

        decoView.addSeries(arcBackTrack);

        SeriesItem seriesItem1 = new SeriesItem.Builder(color)
                .setRange(0, mSeriesMax, 0)
                .setInterpolator(interpolator)
                .setLineWidth(Utility.getDimension(12f, getApplicationContext()))
                .setSpinDuration(5000)
                .setSpinClockwise(false)
                .build();

        mSeries1Index = decoView.addSeries(seriesItem1);

    }

    /**
     * Create a series of events that build a demonstration of moving and positioning
     * the arcs in the DecoView
     *
     * @param decoView
     */
    private void setupEvents(final DecoView decoView) {
        decoView.executeReset();

        decoView.addEvent(new DecoEvent.Builder(15).setIndex(mSeries1Index).setDelay(1000).build());
        decoView.addEvent(new DecoEvent.Builder(40).setIndex(mSeries1Index).setDelay(5000).build());
        decoView.addEvent(new DecoEvent.Builder(50).setIndex(mSeries1Index).setDelay(8000).build());
        decoView.addEvent(new DecoEvent.Builder(0).setIndex(mSeries1Index).setDelay(13000).build());
        decoView.addEvent(new DecoEvent.Builder(20).setIndex(mSeries1Index).setDelay(16000).build());
        decoView.addEvent(new DecoEvent.Builder(25).setIndex(mSeries1Index).setDelay(19000).build());
        decoView.addEvent(new DecoEvent.Builder(0).setIndex(mSeries1Index).setDelay(22000)
                .setListener(new DecoEvent.ExecuteEventListener() {
                    @Override
                    public void onEventStart(DecoEvent event) {

                    }

                    @Override
                    public void onEventEnd(DecoEvent event) {
                    }
                })
                .build());
    }
}